#include "sim_key.h"
#include "log.h"

#ifndef USER_SDL3
#include<easyx.h>
#include<conio.h>

uint8_t SIM_Key_Scan() {
    uint8_t key = 0;
    if (_kbhit()) {
        char keys = _getch();
        switch (keys) {
            //上键
            case 72:
            case 'w':
            case 'W':
                key = SIM_KEY_UP;
                break;

            case 80:
            case 's':
            case 'S':
                key = SIM_KEY_DOWN;
                break;

                //左键
            case 75:
            case 'a':
            case 'A':
                key = SIM_KEY_LEFT;
                break;

                //右键
            case 77:
            case 'd':
            case 'D':
                key = SIM_KEY_RIGHT;
                break;
            case 'q':
            case 'Q':
                key = SIM_KEY_SET;
                break;
            case 'e':
            case 'E':
                key = SIM_KEY_ENABLE;
                break;
            case 'r':
            case 'R':
                key = SIM_KEY_RESET;
                break;
        }
    }
    return key;
}

uint8_t SIM_Key_UP(uint8_t) {
    return GetAsyncKeyState(VK_UP);
}

uint8_t SIM_Key_DOWN(uint8_t l) {
//    uint8_t k=GetAsyncKeyState(VK_DOWN);
//    LOGT("SIM","KEYID:%d-DOWN:%d",l,k);
    return GetAsyncKeyState(VK_DOWN);
}

uint8_t SIM_Key_LEFT(uint8_t) {
    return GetAsyncKeyState(VK_LEFT);
}

uint8_t SIM_Key_RIGHT(uint8_t) {
    return GetAsyncKeyState(VK_RIGHT);
}

uint8_t SIM_Key_ENABLE(uint8_t) {
    return GetAsyncKeyState(VK_RETURN);
}

uint8_t SIM_Key_SET(uint8_t) {
    return GetAsyncKeyState(VK_RSHIFT);
}

uint8_t SIM_Key_RESET(uint8_t) {
    return GetAsyncKeyState(VK_END);
}
#else

#include "SDL3/SDL.h"

uint8_t SIM_Key_Scan() {
    SDL_Event event;
    uint8_t key = 0;
    SDL_PollEvent(&event);
    switch (event.type) {
        case SDL_EVENT_QUIT:
            return 0;
        case SDL_EVENT_KEY_DOWN:
            if (event.key.key == SDLK_Q)key = SIM_KEY_SET;
            if (event.key.key == SDLK_UP || event.key.key == SDLK_W)key = SIM_KEY_UP;
            if (event.key.key == SDLK_DOWN || event.key.key == SDLK_S)key = SIM_KEY_DOWN;
            if (event.key.key == SDLK_LEFT || event.key.key == SDLK_A)key = SIM_KEY_LEFT;
            if (event.key.key == SDLK_RIGHT || event.key.key == SDLK_D)key = SIM_KEY_RIGHT;
            if (event.key.key == SDLK_R)key = SIM_KEY_RESET;
            if (event.key.key == SDLK_KP_ENTER || event.key.key == SDLK_E)key = SIM_KEY_ENABLE;
            break;
        default:
            break;
    }
    return key;
}


uint8_t SIM_Key_UP(uint8_t) {
    SDL_Event event;
    uint8_t key = 0;
    SDL_PollEvent(&event);
    switch (event.type) {
        case SDL_EVENT_QUIT:
            return 0;
        case SDL_EVENT_KEY_DOWN:
            if (event.key.key == SDLK_UP || event.key.key == SDLK_W)key = 1;
            break;
        default:
            break;
    }
    return key;
}

uint8_t SIM_Key_DOWN(uint8_t l) {
    SDL_Event event;
    uint8_t key = 0;
    SDL_PollEvent(&event);
    switch (event.type) {
        case SDL_EVENT_QUIT:
            return 0;
        case SDL_EVENT_KEY_DOWN:
            if (event.key.key == SDLK_DOWN || event.key.key == SDLK_S)key = 1;
        break;
        default:
            break;
    }
    return key;
}

uint8_t SIM_Key_LEFT(uint8_t) {
    SDL_Event event;
    uint8_t key = 0;
    SDL_PollEvent(&event);
    switch (event.type) {
        case SDL_EVENT_QUIT:
            return 0;
        case SDL_EVENT_KEY_DOWN:
            if (event.key.key == SDLK_LEFT || event.key.key == SDLK_A)key = 1;
        break;
        default:
            break;
    }
    return key;
}

uint8_t SIM_Key_RIGHT(uint8_t) {
    SDL_Event event;
    uint8_t key = 0;
    SDL_PollEvent(&event);
    switch (event.type) {
        case SDL_EVENT_QUIT:
            return 0;
        case SDL_EVENT_KEY_DOWN:
            if (event.key.key == SDLK_RIGHT || event.key.key == SDLK_D)key = 1;
        break;
        default:
            break;
    }
    return key;
}

uint8_t SIM_Key_ENABLE(uint8_t) {
    SDL_Event event;
    uint8_t key = 0;
    SDL_PollEvent(&event);
    switch (event.type) {
        case SDL_EVENT_QUIT:
            return 0;
        case SDL_EVENT_KEY_DOWN:
            if (event.key.key == SDLK_KP_ENTER || event.key.key == SDLK_E)key = 1;
        break;
        default:
            break;
    }
    return key;
}

uint8_t SIM_Key_SET(uint8_t) {
    SDL_Event event;
    uint8_t key = 0;
    SDL_PollEvent(&event);
    switch (event.type) {
        case SDL_EVENT_QUIT:
            return 0;
        case SDL_EVENT_KEY_DOWN:
            if (event.key.key == SDLK_Q)key = 1;
        break;
        default:
            break;
    }
    return key;
}

uint8_t SIM_Key_RESET(uint8_t) {
    SDL_Event event;
    uint8_t key = 0;
    SDL_PollEvent(&event);
    switch (event.type) {
        case SDL_EVENT_QUIT:
            return 0;
        case SDL_EVENT_KEY_DOWN:
            if (event.key.key == SDLK_R)key = 1;
        break;
        default:
            break;
    }
    return key;
}

#endif
